package com.youlai.boot.modules.policy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.policy.model.PolicyDetailVO;
import com.youlai.boot.modules.policy.model.PolicyForm;
import com.youlai.boot.modules.policy.model.PolicyVO;
import com.youlai.boot.modules.policy.model.PolicyPageQuery;
import com.youlai.boot.modules.policy.service.PolicyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "政策接口")
@RestController
@RequestMapping("/api/v1/policy")
@RequiredArgsConstructor
public class PolicyController {

    private final PolicyService policyService;

    @Operation(summary = "政策分页列表")
    @GetMapping("/page")
    public PageResult<PolicyVO> listPolicies(PolicyPageQuery query) {
        Page<PolicyVO> result = policyService.getPolicyPage(query);
        return PageResult.success(result);
    }

    @Operation(summary = "政策详情")
    @GetMapping("/{id}")
    public Result<PolicyDetailVO> getPolicy(@PathVariable Long id) {
        PolicyDetailVO policy = policyService.getPolicyById(id);
        return Result.success(policy);
    }

    @Operation(summary = "添加政策")
    @PostMapping
    public Result<Long> addPolicy(@Valid @RequestBody PolicyForm policy) {
        Long id = policyService.savePolicy(policy);
        return Result.success(id);
    }

    @Operation(summary = "修改政策")
    @PutMapping("/{id}")
    public Result<Boolean> updatePolicy(
            @PathVariable Long id,
            @Valid @RequestBody PolicyForm policy) {
        policy.setId(id);
        policyService.updatePolicy(policy);
        return Result.success(true);
    }

    @Operation(summary = "删除政策")
    @DeleteMapping("/{id}")
    public Result<Boolean> deletePolicy(@PathVariable Long id) {
        boolean result = policyService.deletePolicy(id);
        return Result.judge(result);
    }
}